home *** CD-ROM | disk | FTP | other *** search
- Path: news.pi.net!Asd18.pi.net
- From: mv@pi.net
- Newsgroups: comp.lang.c
- Subject: Please help ?!
- Date: Thu, 18 Jan 96 20:46:55
- Organization: Planet Internet
- Message-ID: <4dm889$3hs@neptunus.pi.net>
- NNTP-Posting-Host: asd18.pi.net
- X-Newsreader: IBM WebExplorer DLL
-
- Hello everybody,
-
-
- Please take a look at this:
-
- ----------------------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <string.h>
- #include <alloc.h>
-
-
- #define MAXLEN 20
- #define MAXSWITCH 20
-
-
- char *s="dir /w:test/1/2/3/4";
- char *s2, *s3;
- char myswitches[MAXSWITCH][MAXLEN];
-
-
- int get_switches(char *cmd, char *switches[][MAXLEN])
-
-
- {
- int i=0;
- char *cpy;
- char *saved;
-
- if (strlen(cmd)==0)
- {
- return 0;
- }
- cpy=(char *)malloc(MAXLEN);
- if (cpy==NULL)
- {
- printf("Out of memory in %s on line: %d\n",__FILE__,__LINE__);
- exit(EXIT_FAILURE);
- }
- strcpy(cpy,'\0');
- saved=cpy;
- while (*cmd!='\0')
- {
- if (*cmd=='/')
- {
- if (i==MAXSWITCH)
- {
- printf("INT: Too many switches ...\n");
- return 9999;
- }
- *cpy=*cmd;
- do { *cpy++=*cmd++; } while (!strchr(" /-\0",*cmd));
- *cpy='\0';
- cpy=saved;
- strcpy(switches[i],cpy);
- strcpy(cpy,'\0');
- i++;
- }
- if (*cmd=='-')
- {
- if (i==MAXSWITCH)
- {
- printf("INT: Too many switches ...\n");
- return 9999;
- }
- *cpy=*cmd;
- do { *cpy++=*cmd++; } while (!strchr(" /-\0",*cmd));
- *cpy='\0';
- cpy=saved;
- strcpy(switches[i],cpy);
- strcpy(cpy,'\0');
- i++;
- }
- if (!strchr("/-\0",*cmd)) {*cmd++;}
- }
- free(cpy);
- return i;
- }
-
-
- int main (void)
-
-
- {
- int i;
- clrscr();
- i=get_switches(s,myswitches);
- printf("Switches found: %i\n",i);
- for (i=0; i<MAXSWITCH; i++)
- {
-
-
- /*
-
- Here it should print:
-
- Switch: /w:test
- Switch: /1
- etc..
-
- but it prints:
-
- Switch: /w:test
- Switch:
- Switch: /1
- and so on....
- It jjust skips one array index everytime... It seems to go wrong in get_switches()...
- */
- printf("Switch: %s\n",myswitches[i]);
- }
- return 0;
- }
-
-
-
- TTYL,
-
- Martijn....
-
-